package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipDevOpParam;
import cc.comac.mom.bean.EquipDevOpParamBean;
import cc.comac.mom.dao.entity.EquipDevDao;
import cc.comac.mom.dao.entity.EquipDevOpParamDao;
import cc.comac.mom.entity.EquipDevEntity;
import cc.comac.mom.entity.EquipDevOpParamEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
@Component
@Transactional
public class EquipDevOpParamDataHandler extends ServiceImpl<EquipDevOpParamDao, EquipDevOpParamEntity>{

	@Autowired
	private EquipDevOpParamDao equipDevOpParamDao;
	
	@Autowired	
	private EquipDevDao equipDevDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean save(EquipDevOpParam obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevOpParamEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");	
		}
		if(innerData.getDevId() == 0) {
			throw new RuntimeException("主表信息id为空");
		}
		if(innerData.getParamItem()==null || "".equals(innerData.getParamItem())) {
			throw new RuntimeException("参数项为空");
		}
		if(innerData.getDataSource()== null || "".equals(innerData.getDataSource())) {
			throw new RuntimeException("数据源为空");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("param_item",innerData.getParamItem());
		map.put("dev_id", innerData.getDevId());
		map.put("row_status", 1);
		List<EquipDevOpParamEntity> selectByMap = equipDevOpParamDao.selectByMap(map);
		if(selectByMap != null && selectByMap.size()>0) {	
			throw new RuntimeException("该运行参数信息已经存在");
		}		
		baseEntityDataUtil.setBaseEntityData(innerData);
		int insert = equipDevOpParamDao.insert(innerData);
		if(insert<1) {
			throw new RuntimeException("新增失败");
		}
		return resultBean;
	}
	
	public ResultBean update(EquipDevOpParam obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevOpParamEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		if(innerData.getDevId() == 0) {
			throw new RuntimeException("主表信息id为空");
		}
		if(innerData.getParamItem()==null || "".equals(innerData.getParamItem())) {	
			throw new RuntimeException("参数项为空");
		}
		if(innerData.getDataSource()== null || "".equals(innerData.getDataSource())) {
			throw new RuntimeException("数据源为空");
		}
		EquipDevOpParamEntity selectById = equipDevOpParamDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("修改的数据不存在！");
		}
		if(!selectById.getParamItem().equals(innerData.getParamItem())) {
			throw new RuntimeException("参数项不能修改");
		}
		baseEntityDataUtil.updateBaseEntityData(innerData);
		int update = equipDevOpParamDao.updateById(innerData);
		if(update<1) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}
	
	public ResultBean delete(EquipDevOpParam obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevOpParamEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevOpParamEntity selectById = equipDevOpParamDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("删除的数据不存在！");
		}
		baseEntityDataUtil.deleteBaseEntityData(selectById);
		int delete = equipDevOpParamDao.updateById(selectById);
		if(delete<1){
			throw new RuntimeException("删除失败");
		}
		return resultBean;
	}
	public EquipDevOpParamBean selectByCondition(EquipDevOpParam obj) {
    	QueryWrapper<EquipDevOpParamEntity> queryWrapper = new QueryWrapper<EquipDevOpParamEntity>();
    	queryWrapper.eq("row_status",1);
    	if(obj != null && obj.getInnerData() != null) {
    		if(obj.getInnerData().getDevId() != 0) {
    			ArrayList<Long> arrayList = new ArrayList<Long>();
    			arrayList.add(obj.getInnerData().getDevId());
    			EquipDevEntity equipDevEntity = equipDevDao.selectById(obj.getInnerData().getDevId());
    			if(equipDevEntity != null) {
    				arrayList.add(equipDevEntity.getDevCategoryId());
    			}
    			queryWrapper.in("dev_id",arrayList);
    		}
    		if(obj.getInnerData().getParamItem()!= null && !"".equals(obj.getInnerData().getParamItem())) {
    			queryWrapper.eq("param_item",obj.getInnerData().getParamItem());
    		}
    		if(obj.getInnerData().getDataSource()!= null && !"".equals(obj.getInnerData().getDataSource())) {
    			queryWrapper.eq("data_source",obj.getInnerData().getDataSource());
    		}
    	}
        Page<EquipDevOpParamEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
        IPage<EquipDevOpParamEntity> iPage = equipDevOpParamDao.selectPage(page, queryWrapper);
        List<EquipDevOpParamEntity> list = iPage.getRecords();
        if(list != null && list.size()>0) {
        	long pages = iPage.getPages();
        	long total = iPage.getTotal();
        	ArrayList<EquipDevOpParam> arrayList = new ArrayList<EquipDevOpParam>();
        	for(EquipDevOpParamEntity item :list) {
        		EquipDevOpParam equipDevOpParam = new EquipDevOpParam();
        		equipDevOpParam.setInnerData(item);
        	    arrayList.add(equipDevOpParam);
        	}
        	EquipDevOpParamBean equipDevOpParamBean = new EquipDevOpParamBean();
        	equipDevOpParamBean.setList(arrayList);
        	equipDevOpParamBean.setPageNum(obj.getPageNum());
        	equipDevOpParamBean.setPageSize(obj.getPageSize());
        	equipDevOpParamBean.setPages((int) pages);
        	equipDevOpParamBean.setTotal((int) total);
        	return equipDevOpParamBean;
        }
		return null;
		
	}
	
	public EquipDevOpParam getById(long id) {
		// TODO Auto-generated method stub
		 EquipDevOpParamEntity opParamEntity = equipDevOpParamDao.selectById(id);
		if(opParamEntity != null) {
			EquipDevOpParam equipDevOpParam = new EquipDevOpParam();
			equipDevOpParam.setInnerData(opParamEntity);
			return equipDevOpParam;
		}
		return null;
	}
}
